using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Flan.Controls;

/// <summary>
/// Same concept as example 04.
/// 
/// UpdatePanel1 is refreshed only under the following conditions:
///  * A command other than "Select" or "AddRecord" is fired (example: Paging)
///  * Anytime a record is updated, added or deleted
/// 
/// The popup is hidden after the following conditions:
///  * A record is inserted successfully
///  * A record is deleted successfully
///  * The Cancel button is clicked
/// </summary>
public partial class _08_Master_Details_AddDelEdit : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
        System.Threading.Thread.Sleep(500);
        UpdatePanelPopupExtender.RegisterCloseControl(divClose, UpdatePanel2);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {

        bool showPopup = false;
        DetailsView1.AutoGenerateInsertButton = false;

        switch (e.CommandName) {
            case "Select":
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                DetailsView1.HeaderText = "Shipper Details";
                showPopup = true;
                break;
            case "AddRecord":
                DetailsView1.ChangeMode(DetailsViewMode.Insert);
                DetailsView1.AutoGenerateInsertButton = true;
                DetailsView1.HeaderText = "Add a new shipper";
                showPopup = true;
                break;
            default:
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                UpdatePanel1.Update();
                break;
        }

        if (showPopup) {
            Control c = e.CommandSource as Control;
            if (c != null) {
                UpdatePanelPopupExtender2.ShowAt(c);
                UpdatePanel2.Update();
            }
        }
    }

    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e) {
        GridView1.DataBind();
        UpdatePanel1.Update();
    }

    protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) {
        //Hide popup if the user clicks on Cancel when in insert mode
        if ((DetailsView1.CurrentMode == DetailsViewMode.Insert) &&
            (e.CommandName == "Cancel")) {
            GridView1.SelectedIndex = -1;
            UpdatePanelPopupExtender2.Hide();
        }
    }
    protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e) {
        GridView1.SelectedIndex = -1;
        GridView1.DataBind();
        UpdatePanel1.Update();
        UpdatePanelPopupExtender2.Hide();
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) {
        GridView1.DataBind();
        //Go to the last page to show inserted record
        GridView1.PageIndex = GridView1.PageCount - 1;
        UpdatePanel1.Update();
        UpdatePanelPopupExtender2.Hide();
    }

    /// <summary>
    /// Add js delete confirm dialog
    /// </summary>
    protected void DetailsView1_DataBound(object sender, EventArgs e) {
        if (DetailsView1.CurrentMode != DetailsViewMode.ReadOnly) {
            return;
        }

        foreach (DetailsViewRow dr in DetailsView1.Rows) {
            if (dr.HasControls() && dr.Controls[0].HasControls()) {
                foreach (Control c in dr.Controls[0].Controls) {
                    if (c is LinkButton) {
                        LinkButton li = (LinkButton)c;
                        if (li.CommandName == "Delete") {
                            li.OnClientClick = "return confirm('Are you sure you want to delete this record?')";
                        }
                    }
                }
            }
        }
    }
}
